<?php

namespace app\teacher\controller;

use app\teacher\security\TchCheck as TchCheckLogic;
use app\teacher\model\Thesis as ThesisModel;
use app\teacher\model\AddThesis as AddThesisModel;
use app\teacher\security\Auth as AuthLogic;
use think\Controller;
use think\Session;

class AddSuggest extends Controller
{
    /**
     * 登录界面显示，自动登录
     * @access public
     * @return mixed 返回登录视图
     */
    public function addSuggest($tisId='')
    {
        ///检测是否在审核阶段
        $config = getSrvConfig();
        if ($config) {
            if ($config['srvStatus'] === 2) {
                $tchCheck = new TchCheckLogic();
                //检测教师登录情况
                if ($tchCheck->check()) {
                    //检测是否有审核员权限
                    $auth = new AuthLogic();
                    //获取教师工号
                    $username = Session::get('token')['tchUsername'];
                    if ($auth->check('review', $username)) {
                        $status=getSrvConfig()['srvStatus'];
                        if($status==2){
                            $this->assign('status',$status);
                        }
                        $thesis=new ThesisModel();
                        if($result=$thesis::where('tisId',$tisId)->find()){
                            if($result['tisAuthor']!=$username){
                                $this->assign('thesis',$result);
                                return $this->fetch('addSuggest/addSuggest');
                            }else{
                                return "您不能审核自己的题目";
                            }
                        }else{
                            return "没有查询到题目，请稍后再试";
                        }
                    }else{
                        return "500：您没审核的权限";
                    }
                }else{
                    return "600：您的登录信息出现问题，请重新登录"
                    . "<br>" . "1.您的帐号在其他地方登录"
                    . "<br>" . "2.您在线时间过长（5小时以上）"
                    . "<br>" . "3.令牌验证错误";
                }
            }else{
                return "现在不是审核阶段";
            }
        }else{
            return "配置查询出错，请稍后再试";
        }
    }


    /**
     * 添加新题目功能
     * @access public
     * @param string $tisId 题目编号
     * @param string $tisSuggest 题目建议
     * @return mixed 返回执行结果
     */
    public function do_addSuggest($tisId = '', $tisSuggest = '')
    {
        //检测是否在审核阶段
        $config = getSrvConfig();
        if ($config) {
            if ($config['srvStatus'] === 2) {
                $tchCheck = new TchCheckLogic();
                //检测教师登录情况
                if ($tchCheck->check()) {
                    //检测是否有审核员权限
                    $auth = new AuthLogic();
                    //获取教师工号
                    $username = Session::get('token')['tchUsername'];
                    if ($auth->check('review', $username)) {
                        $data = [
                            'tisId' => $tisId,
                            'tisSuggest' => $tisSuggest,
                            'UpdateTime' => date('y-m-d h:i:s', time()),
                        ];
                        //实例化题目模型类
                        $thesis = new AddThesisModel();
                        if ($thesis->isUpdate(true)->save($data)) {
                            return 1;
                        } else {
                            return "审核失败，请稍后再试";
                        }
                    } else {
                        return "500：您没审核的权限";
                    }
                } else {
                    return "600：您的登录信息出现问题，请重新登录"
                    . "<br>" . "1.您的帐号在其他地方登录"
                    . "<br>" . "2.您在线时间过长（5小时以上）"
                    . "<br>" . "3.令牌验证错误";
                }
            } else {
                return "现在不是审核阶段，请不要提交建议";
            }
        } else {
            return "配置查询出错，请稍后再试";
        }
    }
}